package soccer.view;

import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTabbedPane;

import soccer.model.matches.Match;
import javax.swing.border.EtchedBorder;
import java.awt.Color;

public class TeamStatisticPanel extends JPanel {
	private JPanel generalTeamStatisticPanel;
	private JTable mostGoalTeamTable;
	private JTable mostGoalAgainstTeam;
	private JTable mostWinTeams;
	private JTable mostLostTeams;
	private JComboBox weekSelect;
	private JButton BackToStatisticButton;
	private JPanel specificTeamStatisticPanel;
	private JComboBox teamListComboBox;
	private JTable winMatchOfTeamTable;
	private JTable lostMatchOfTeamTable;
	private JTable drawMatchOfTeamTable;

	/**
	 * Create the panel.
	 */
	public TeamStatisticPanel() {
		setLayout(null);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(33, 24, 600, 450);
		add(tabbedPane);
		
		generalTeamStatisticPanel = new JPanel();
		tabbedPane.addTab("General Team Statistic", null, generalTeamStatisticPanel, null);
		generalTeamStatisticPanel.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(195, 36, 136, 126);
		generalTeamStatisticPanel.add(scrollPane);
		
		mostGoalTeamTable = new JTable();
		mostGoalTeamTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Team Name", "Goal"
			}
		));
		scrollPane.setViewportView(mostGoalTeamTable);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(357, 209, 136, 137);
		generalTeamStatisticPanel.add(scrollPane_1);
		
		mostGoalAgainstTeam = new JTable();
		mostGoalAgainstTeam.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Team Name", "Goal"
			}
		));
		scrollPane_1.setViewportView(mostGoalAgainstTeam);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		scrollPane_2.setBounds(357, 32, 136, 137);
		generalTeamStatisticPanel.add(scrollPane_2);
		
		mostWinTeams = new JTable();
		mostWinTeams.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null},
			},
			new String[] {
				"Team Name", "Win"
			}
		));
		scrollPane_2.setViewportView(mostWinTeams);
		
		JScrollPane scrollPane_3 = new JScrollPane();
		scrollPane_3.setBounds(195, 209, 136, 137);
		generalTeamStatisticPanel.add(scrollPane_3);
		
		mostLostTeams = new JTable();
		mostLostTeams.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Team Name", "lost"
			}
		));
		scrollPane_3.setViewportView(mostLostTeams);
		
		JLabel lblMostGoalsScorer = new JLabel("Most Goals Scorer Teams");
		lblMostGoalsScorer.setHorizontalAlignment(SwingConstants.CENTER);
		lblMostGoalsScorer.setBounds(195, 11, 136, 14);
		generalTeamStatisticPanel.add(lblMostGoalsScorer);
		
		JLabel lblMostAgainstGoals = new JLabel("Most Against Goals Scorer Teams");
		lblMostAgainstGoals.setHorizontalAlignment(SwingConstants.CENTER);
		lblMostAgainstGoals.setBounds(345, 195, 170, 14);
		generalTeamStatisticPanel.add(lblMostAgainstGoals);
		
		JLabel lblMostGoalsWin = new JLabel("Most  Win Teams");
		lblMostGoalsWin.setHorizontalAlignment(SwingConstants.CENTER);
		lblMostGoalsWin.setBounds(357, 11, 136, 14);
		generalTeamStatisticPanel.add(lblMostGoalsWin);
		
		JLabel lblMostLostteams = new JLabel("Most lostTeams");
		lblMostLostteams.setHorizontalAlignment(SwingConstants.CENTER);
		lblMostLostteams.setBounds(195, 195, 136, 14);
		generalTeamStatisticPanel.add(lblMostLostteams);
		
		weekSelect = new JComboBox();
		weekSelect.setBounds(53, 67, 60, 20);
		generalTeamStatisticPanel.add(weekSelect);
		
		JLabel lblWeek = new JLabel("Week :");
		lblWeek.setBounds(10, 70, 47, 14);
		generalTeamStatisticPanel.add(lblWeek);
		
		BackToStatisticButton = new JButton("Back to Statistic");
		BackToStatisticButton.setBounds(297, 353, 109, 23);
		generalTeamStatisticPanel.add(BackToStatisticButton);
		
		specificTeamStatisticPanel = new JPanel();
		specificTeamStatisticPanel.setBorder(new EtchedBorder(EtchedBorder.RAISED, new Color(169, 169, 169), new Color(47, 79, 79)));
		tabbedPane.addTab("Specific Team Statistic", null, specificTeamStatisticPanel, null);
		specificTeamStatisticPanel.setLayout(null);
		
		teamListComboBox = new JComboBox();
		teamListComboBox.setBounds(239, 67, 112, 20);
		specificTeamStatisticPanel.add(teamListComboBox);
		
		JLabel teamListComboboxLabel = new JLabel("Select a team to get statistic information");
		teamListComboboxLabel.setHorizontalAlignment(SwingConstants.CENTER);
		teamListComboboxLabel.setBounds(192, 42, 207, 14);
		specificTeamStatisticPanel.add(teamListComboboxLabel);
		
		JScrollPane winMatchOfTeamScrollPane = new JScrollPane();
		winMatchOfTeamScrollPane.setBounds(10, 135, 269, 95);
		specificTeamStatisticPanel.add(winMatchOfTeamScrollPane);
		
		winMatchOfTeamTable = new JTable();
		winMatchOfTeamTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Match Date", "Home", "Skor", "Away"
			}
		));
		winMatchOfTeamScrollPane.setViewportView(winMatchOfTeamTable);
		
		JScrollPane lostMatchOfTeamScrollPane = new JScrollPane();
		lostMatchOfTeamScrollPane.setBounds(318, 135, 262, 95);
		specificTeamStatisticPanel.add(lostMatchOfTeamScrollPane);
		
		lostMatchOfTeamTable = new JTable();
		lostMatchOfTeamTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Match Date", "Home", "Skor", "Away"
			}
		));
		lostMatchOfTeamScrollPane.setViewportView(lostMatchOfTeamTable);
		
		JLabel lblTheMatchesThat = new JLabel("The matches that the team won");
		lblTheMatchesThat.setHorizontalAlignment(SwingConstants.CENTER);
		lblTheMatchesThat.setBounds(34, 122, 198, 14);
		specificTeamStatisticPanel.add(lblTheMatchesThat);
		
		JLabel lblTheMatchesThat_1 = new JLabel("The matches that the team lost");
		lblTheMatchesThat_1.setHorizontalAlignment(SwingConstants.CENTER);
		lblTheMatchesThat_1.setBounds(343, 122, 198, 14);
		specificTeamStatisticPanel.add(lblTheMatchesThat_1);
		
		JScrollPane drawMatchOfTeamScrollPane = new JScrollPane();
		drawMatchOfTeamScrollPane.setBounds(192, 251, 269, 95);
		specificTeamStatisticPanel.add(drawMatchOfTeamScrollPane);
		
		drawMatchOfTeamTable = new JTable();
		drawMatchOfTeamTable.setModel(new DefaultTableModel(
				new Object[][] {
				},
				new String[] {
					"Match Date", "Home", "Skor", "Away"
				}
			));
		drawMatchOfTeamScrollPane.setViewportView(drawMatchOfTeamTable);

	}
	
	public void addWinNumberOfTeams(List l){
		DefaultTableModel defaultTableModel  = (DefaultTableModel) mostWinTeams.getModel();
		resetTeamTable(defaultTableModel);
		for(int i = 0; i < l.size(); i++){
			Map winMap = (Map) l.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) winMap.get("teamName"));;
			v.add((Comparable) winMap.get("win"));
			defaultTableModel.addRow(v);
		}
	}
	public void addLostNumberOfTeams(List l){
		DefaultTableModel defaultTableModel  = (DefaultTableModel) mostLostTeams.getModel();
		resetTeamTable(defaultTableModel);
		for(int i = 0; i < l.size(); i++){
			Map lostMap = (Map) l.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) lostMap.get("teamName"));;
			v.add((Comparable) lostMap.get("lost"));
			defaultTableModel.addRow(v);
		}
	}
	public void addMostGoalsOfTeams(List l){
		DefaultTableModel defaultTableModel  = (DefaultTableModel) mostGoalTeamTable.getModel();
		resetTeamTable(defaultTableModel);
		for(int i = 0; i < l.size(); i++){
			Map winMap = (Map) l.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) winMap.get("teamName"));;
			v.add((Comparable) winMap.get("attigi"));
			defaultTableModel.addRow(v);
		}
	}
	public void addMostGoalAgainstOfTeams(List l){
		DefaultTableModel defaultTableModel  = (DefaultTableModel) mostGoalAgainstTeam.getModel();
		resetTeamTable(defaultTableModel);
		for(int i = 0; i < l.size(); i++){
			Map winMap = (Map) l.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) winMap.get("teamName"));;
			v.add((Comparable) winMap.get("yedigi"));
			defaultTableModel.addRow(v);
		}
	}
	// specific team statistic panelindeki table  match ları eklemek için 
	public void addMatchOfSpecialTeamTable(List<Match> matchList ,  JTable table){
		DefaultTableModel defaultTableModel = (DefaultTableModel) table.getModel();
		resetTeamTable(defaultTableModel);
		for(int i = 0; i <  matchList.size() ; i++){
			Match matchMap = matchList.get(i);
			Vector<Comparable> v = new Vector<Comparable>();
			v.add((Comparable) matchMap.getMatchDate());
			v.add((Comparable) matchMap.getHomeTeam().getTeamName());
			v.add((Comparable) (matchMap.getMatchDetail().getHomeSkor()+
					":"+matchMap.getMatchDetail().getAwaySkor()));
			v.add((Comparable) matchMap.getAwayTeam().getTeamName());
			defaultTableModel.addRow(v);
		}
	}
	
	public void resetTeamTable(DefaultTableModel model){
		while(model.getRowCount() > 0){
			model.removeRow(0);
		}
	}
	
	  public void fillWeekCombobox(int week){
	    	Object[] items  = new Object[week];
	    	for(int i = 0; i < week; i++){
	    		items[i] = i+1;
	    	}
	    	DefaultComboBoxModel boxModel = new DefaultComboBoxModel(items);
	    	weekSelect.setModel(boxModel);
	    	weekSelect.setSelectedIndex(week-1);
	    }
     public void fillTeamsToTeamsCombobox(List teamsName){
    	 Object[] items = new Object[teamsName.size()];
	     
 	    for(int i = 0; i < items.length; i++){
 	    	Map item = (Map) teamsName.get(i);
 	    	items[i] = item.get("teamName");
 	    }
 	    DefaultComboBoxModel defaultComboBoxModel = new DefaultComboBoxModel(items);
 	    teamListComboBox.setModel(defaultComboBoxModel);
	  }
	public void addWeekSelectComboboxItemListener(ItemListener l){
		weekSelect.addItemListener(l);
	}
    public void addBackToStatisticButtonActionListener(ActionListener l){
    	BackToStatisticButton.addActionListener(l);
    }
	public JPanel getGeneralTeamStatisticPanel() {
		return generalTeamStatisticPanel;
	}
    public void addTeamSelectComboboxItemListener(ItemListener l){
    	teamListComboBox.addItemListener(l);
    }
	public void setGeneralTeamStatisticPanel(JPanel generalTeamStatisticPanel) {
		this.generalTeamStatisticPanel = generalTeamStatisticPanel;
	}

	public JTable getMostGoalTeamTable() {
		return mostGoalTeamTable;
	}

	public void setMostGoalTeamTable(JTable mostGoalTeamTable) {
		this.mostGoalTeamTable = mostGoalTeamTable;
	}

	public JTable getMostGoalAgainstTeam() {
		return mostGoalAgainstTeam;
	}

	public void setMostGoalAgainstTeam(JTable mostGoalAgainstTeam) {
		this.mostGoalAgainstTeam = mostGoalAgainstTeam;
	}

	public JTable getMostWinTeams() {
		return mostWinTeams;
	}

	public void setMostWinTeams(JTable mostWinTeams) {
		this.mostWinTeams = mostWinTeams;
	}

	public JTable getMostLostTeams() {
		return mostLostTeams;
	}

	public void setMostLostTeams(JTable mostLostTeams) {
		this.mostLostTeams = mostLostTeams;
	}

	public JComboBox getWeekSelect() {
		return weekSelect;
	}

	public void setWeekSelect(JComboBox weekSelect) {
		this.weekSelect = weekSelect;
	}

	public JButton getBackToStatisticButton() {
		return BackToStatisticButton;
	}

	public void setBackToStatisticButton(JButton backToStatisticButton) {
		BackToStatisticButton = backToStatisticButton;
	}

	public JPanel getSpecificTeamStatisticPanel() {
		return specificTeamStatisticPanel;
	}

	public void setSpecificTeamStatisticPanel(JPanel specificTeamStatisticPanel) {
		this.specificTeamStatisticPanel = specificTeamStatisticPanel;
	}

	public JComboBox getTeamListComboBox() {
		return teamListComboBox;
	}

	public void setTeamListComboBox(JComboBox teamListComboBox) {
		this.teamListComboBox = teamListComboBox;
	}

	public JTable getWinMatchOfTeamTable() {
		return winMatchOfTeamTable;
	}

	public void setWinMatchOfTeamTable(JTable winMatchOfTeamTable) {
		this.winMatchOfTeamTable = winMatchOfTeamTable;
	}

	public JTable getLostMatchOfTeamTable() {
		return lostMatchOfTeamTable;
	}

	public void setLostMatchOfTeamTable(JTable lostMatchOfTeamTable) {
		this.lostMatchOfTeamTable = lostMatchOfTeamTable;
	}

	public JTable getDrawMatchOfTeamTable() {
		return drawMatchOfTeamTable;
	}

	public void setDrawMatchOfTeamTable(JTable drawMatchOfTeamTable) {
		this.drawMatchOfTeamTable = drawMatchOfTeamTable;
	}
	
}
